package studio.banner.reference.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import studio.banner.reference.entity.UserInfo;
import studio.banner.reference.entity.UserLogin;
import studio.banner.reference.mapper.UserInfoMapper;
import studio.banner.reference.mapper.UserLoginMapper;
import studio.banner.reference.util.ShiroUtil;

import java.util.List;

@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserLoginMapper userLoginMapper;
    private UserInfo userInfo = null;

    //	查询是否已经有这个账号了
    public Integer findByUserName(String studentid) {
        userInfo = new UserInfo();
        userInfo.setStudentid(studentid);
//		根据单列查询
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("studentid", studentid);
        UserInfo result = userInfoMapper.selectOne(wrapper);
        try {
            if (result.getId() != null) {
                System.out.println("-----" + result.getId());
                return result.getId();
            }
//			  0表示为空

        } catch (Exception e) {
            // TODO: handle exception

        }
        return 0;
    }

    //查询是否已经有这个账号了返回一个bean
    public UserInfo findUserName(String studentid) {
        userInfo = new UserInfo();
        userInfo.setStudentid(studentid);
//根据单列查询
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("studentid", studentid);
        UserInfo result = userInfoMapper.selectOne(wrapper);
        return result;
    }

    //根据用户名查询返回这个对象
    public UserInfo findUserLoginandreturnUserLogin(String studentid) {
        UserInfo userInfo = new UserInfo();
        userInfo.setStudentid(studentid);
//		根据单列查询
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("studentid", studentid);
        UserInfo result = userInfoMapper.selectOne(wrapper);
        try {
            if (result != null) {
                return result;
            }
//			  0表示为空

        } catch (Exception e) {
            // TODO: handle exception

        }

        return null;
    }

    /*
     * 	插入注册信息,以及写入用户密码
     */
    public Integer insertUserandLogin(UserInfo user, String password) {
        UserLogin ul = new UserLogin();
        ul.setUsername(user.getStudentid());
        ul.setPassword(ShiroUtil.getMd5(password, user.getStudentid()));
        userInfoMapper.insert(user);
        ul.setUserinfo_id(user.getId());
        return userLoginMapper.insert(ul);
    }

    /*
     * 查询数据表所有记录
     */
    public List<UserInfo> allList() {
        IPage<UserInfo> all = userInfoMapper.selectPage(new Page<UserInfo>(3, 2), null);
        List<UserInfo> infos = all.getRecords();
        return infos;
    }

    /*
     * 删除单个用户信息
     *
     */
    public Integer deleteone(Integer id) {
        return userInfoMapper.deleteById(id);
    }

    /*
     * 删除多个用户信息
     */
    public Integer deletelist(List<Integer> ids) {
        return userInfoMapper.deleteBatchIds(ids);
    }

    /*
     * 修改用户信息
     */
    public Integer update(UserInfo user) {
        return userInfoMapper.updateById(user);
    }

    /*
     * 插入数据
     */
    public Integer insert(UserInfo user) {
        return userInfoMapper.insert(user);
    }

    /*
     * 按照studentid 和name属性进行模糊查询
     */
    public List<UserInfo> selectstudentidandname(UserInfo user) {
        //条件封装
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(user.getStudentid() != null, "studentid", user.getStudentid());
        queryWrapper.like(user.getName() != null, "name", user.getName());
        List<UserInfo> userList = userInfoMapper.selectList(queryWrapper);
        return userList;
    }
}
